<?php
/**
 * 仓库
 * Created by PhpStorm.
 * User: echa
 * Date: 2018/3/12
 * Time: 9:35
 */
namespace App\Http\Controllers\WarehouseManage;
use App\Http\Controllers\ApiController;
use APP\Services\Warehouse\AvailableQueryService;
use App\Services\Warehouse\WarehouseLocalService;
use Illuminate\Http\Request;

class WarehouseLocalController extends ApiController{
    protected $WarehouseLocalService;
    protected $AvailableQueryService;
   public function __construct(WarehouseLocalService $warehouseLocalService,AvailableQueryService $availableQueryService)
   {
       $this->WarehouseLocalService=$warehouseLocalService;
       $this->AvailableQueryService=$availableQueryService;
   }

   /*查看当前商品的库存情况*/
   public function GetGoodsInventory(Request $request){
       $goods_sn=$request->goods_sn;
       $wid=$request->wid;
       $info=$this->WarehouseLocalService->GetGoodsInventory($goods_sn,$wid);
       if($info){
           return $info;
       }else{
           return false;
       }
   }

   /**
   *    检查金蝶系统商品物料可用量是否充足
    */
   public function CheckGoodsInventoryAVBQty(Request $request){
       $goods_sn=$request->goods_sn;
       $wid=$request->wid;
       $quantity=$request->quantity;
       if(empty($quantity)){
           $quantity=1;
       }
       $AVBQty=$this->WarehouseLocalService->CheckGoodsInventoryAVBQty($goods_sn,$wid,$quantity);
       if($AVBQty){
           return $AVBQty;
       }else{
           return false;
       }
   }

   /**
   *    根据WID获取当前仓库基本信息
    */
   public function GetWarehouseInfo(Request $request){
       $wid=$request->wid;
       $info=$this->WarehouseLocalService->GetWarehouseInfo($wid);
       if($info){
           return $info;
       }else{
           return false;
       }
   }


    /***
     * 根据商品货号和对应所属仓库编号查询商品库存情况
     * 参数 goodsdata ={"wid":"8","goods":["0080687504565","0080687504565","0080687504565"]}
     * @param Request $request
       @author echa
     * @return mixed
     */
   public function GetGoodsWarehouse(Request $request){
       $data=$request->goodsdata;
       if(empty($data)){
           return $this->failed('非法请求');
       }
       $data=json_decode($data,true);
       if(empty($data)){
           return $this->failed('参数错误');
       }
       $wid=$data['wid'];
       try{
           $k3_number=$this->WarehouseLocalService->CheckWarehouseBindingExist($wid);
           if(!$k3_number){
               return $this->failed('该仓库未进行绑定');
           }
           $goods_snarr=$data['goods'];
           $return=$this->AvailableQueryService->GetGoodsWarehouse($goods_snarr,$k3_number);
           if($return){
               return $this->success($return);
           }else{
               return $this->failed('该商品查无数据');
           }
       }catch (\Exception $exception){
           return $this->failed($exception->getMessage());
       }
   }

    /***
     * @param Request $request
    @author echa
     * @return mixed
     */
   public function CheckGoodsUsableWarehouse(Request $request){
       $data=$request->goodsdata;
       if(empty($data)){
           return $this->failed('非法请求');
       }
       $data=json_decode($data,true);
       if(empty($data)){
           return $this->failed('参数错误');
       }
       $wid=$data['wid'];
       $goods_snarr=$data['goods'];
       if(empty($goods_snarr)){
           return $this->failed('请传入商品货号');
       }
       try{
           $return=$this->WarehouseLocalService->CheckGoodsUsableWarehouse($goods_snarr,$wid);
           if($return){
               return $this->success($return);
           }else{
               return $this->failed('该商品查无数据');
           }
       }catch (\Exception $exception){
           return $this->failed($exception->getMessage());
       }
   }


}